<template>
	<view>
		<uv-navbar leftText="返回" title="问诊单" :fixed="false" @leftClick="backClick"></uv-navbar>
		
		<uv-subsection
			:list="list" 
			:current="current"
			bgColor="#e1efe4"
			activeColor="#2c9c3c"
			custom-style="height: 60rpx;border-radius: 30rpx;" 
			custom-item-style="border-radius: 30rpx;">
		</uv-subsection>
			
		<!-- 患者信息 -->
		<view v-if="current === 0">
			<Patient @patientNext='patientNexts' ref="getRxInfo"></Patient>
		</view>
		
		<!-- 处方单 -->
		<view v-if="current === 1">
			<Rx @rxBack='rxBacks' @rxNext='rxNexts'></Rx>
		</view>
		
		<!-- 复诊信息 -->
		<view v-if="current === 2">
			<Visit @visitBack='visitBacks' @visitNext='visitNexts'></Visit>
		</view>
		
	</view>
</template>

<script>
	import Patient from '@/pages/medic/create_rx/components/patient.vue'
	import Rx from '@/pages/medic/create_rx/components/rx.vue'
	import Visit from '@/pages/medic/create_rx/components/visits.vue'
	export default {
		components: {
			Patient, Rx, Visit
		},
		data() {
			return {
				list: ['患者信息', '处方单', '复诊信息'],
				current: 0,
				rx_info: {
					name: '',
					age: '',
					sex: '',
					mobile: ''
				}
			}
		},
		destroyed() {
			this.$store.dispatch('clear_rx')
		},
		methods: {
			backClick() {
				uni.navigateBack()
				this.$store.dispatch('clear_rx')
			},
			hansValues() {
				this.rx_info = this.$store.state.rx
				const hasEmptyValue = Object.values(this.rx_info).some(value => value === '')	// 当有一个值为空时不允许下一步
				if (hasEmptyValue) {
					uni.showToast({ title: '完善患者信息', icon: 'error' })
					setTimeout(function () { uni.hideLoading() }, 2000);
					return false
				} else {
					this.$store.dispatch('set_rx', this.rx_info)	// 缓存数据
					return true
				}
			},
			patientNexts() {
				if (this.hansValues()) {
					const regExp = /^1[3456789]\d{9}$/; // 使用正则表达式进行校验
					if (!regExp.test(this.rx_info.mobile)) {
						uni.showToast({
							icon: 'error',
							title: '手机号码不正确'
						})
					} else {
						this.current = this.current + 1
					}
					
				}
			},
			rxBacks() {
				this.current = this.current - 1
			},
			rxNexts() {
				this.current = this.current + 1
			},
			visitBacks() {
				this.current = this.current - 1
			},
			// 提交数据 - 图片上传到uniCloud云存储
			visitNexts(rx_info_data) {
				// 上传图片到
				uni.showLoading({ title: '加载中...' })
				var _this = this
				uniCloud.uploadFile({
				  filePath: rx_info_data.canvas.tempImagePath,
				  cloudPath: `${Number(new Date())}.png`, // 上传到云存储的路径和文件名
				}).then(res => {
					console.log('上传文件的回调：', res)
					if (res.success) {
						// 提交数据
						var userinfo = _this.$store.state.userinfo
						rx_info_data.rx_image_url = res.fileID
						rx_info_data.rx_image_width = rx_info_data.canvas.width
						rx_info_data.rx_image_hight = rx_info_data.canvas.hight
						rx_info_data.orderId = Number(new Date())	// 处方单ID
						rx_info_data.clinic = {
							clinicId: userinfo.clinic._id,		// 问诊诊所ID
							medic_userId: userinfo.userId,	// 问诊医生ID
							medic_name: userinfo.name,		// 问诊医生姓名
							medic_mobile: userinfo.mobile,		// 问诊医生手机号
							clinic_name: userinfo.clinic.clinic,		// 问诊诊所
							addressCity: userinfo.clinic.addressCity,		// 问诊诊所
							addressCounty: userinfo.clinic.addressCounty,		// 问诊诊所
							addressProvince: userinfo.clinic.addressProvince,		// 问诊诊所
							address: userinfo.clinic.address		// 问诊诊所
						}
						delete rx_info_data.canvas		// 删除掉临时路径后再提交
						
						_this.$api.create_rx(rx_info_data).then((res) => {
							
							if ( res.data.update === 0 ) {
								console.log('新增了处方单, 配置自动发送短信', res.data)
								uni.reLaunch({ url:'/pages/medic/medic' })
								this.$store.dispatch('clear_rx')
							}
							
							if ( res.data.update === 1 ) {
								console.log('编辑了处方单, 判断是否需要删除或重设自动发送短信的时间', res.data)
								this.backClick()
							}
							
							setTimeout(function () { uni.hideLoading() }, 1000);
							
						}).catch(err => {
							setTimeout(function () { uni.hideLoading() }, 1000);
							uni.showToast({ title: '服务错误', icon: 'error', duration: 3000 })
						});
					} else {
						setTimeout(function () { uni.hideLoading() }, 1000);
						uni.showToast({ title: '处方上传失败', icon: 'error', duration: 3000 })
					}
					
				}).catch(err => {
					setTimeout(function () { uni.hideLoading() }, 1000);
					uni.showToast({ title: '服务错误', icon: 'error', duration: 3000 })
				});
				
				
				
			}
		}
	}
</script>

<style lang="scss">
	
</style>
